package com.chenss.dragonserapp.utils;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 使用正则表达式进行表单验证
 *
 */

public class RegexValidateUtil {
    static boolean flag = false;
    static String regex = "";

    public static boolean check(String str, String regex) {
        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }


    /**
     * 将手机号中间4位替换成星星
     * @param pNumber
     * @return
     */
    public static String changePhoneToStar(String pNumber){
        if(!TextUtils.isEmpty(pNumber) && pNumber.length() > 6 ){
            StringBuilder sb  =new StringBuilder();
            for (int i = 0; i < pNumber.length(); i++) {
                char c = pNumber.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }

            return sb.toString();
        }

        return pNumber;

    }



    /**
     * 验证非空
     *
     * @return
     */
    public static boolean checkNotEmputy(String notEmputy) {
        regex = "^\\s*$";
        return check(notEmputy, regex) ? false : true;
    }

    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        String regex = "^\\w+[-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$ ";
        return check(email, regex);
    }

    /**
     * 验证手机号码
     *
     移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
     * 电信号段: 133,149,153,170,173,177,180,181,189
     *
     * @param cellphone
     * @return
     */
    public static boolean checkCellphone(String cellphone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(19[9])|(17[0,1,3,5,6,7,8]))\\d{8}$";
        return check(cellphone, regex);
    }

    /**
     * 验证固话号码
     *
     * @param telephone
     * @return
     */
    public static boolean checkTelephone(String telephone) {
        String regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
        return  check(telephone, regex);
    }
    /**
     * 登录时密码只验证长度及是否为特殊字符
     */
    public static boolean checkLoginPassword(String pwd) {
        boolean isLenMatch=false;
        if (checkNotEmputy(pwd) && pwd.length() >= 8 && pwd.length() <= 16) {
            isLenMatch=true;
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight =  pwd.matches(regex);
        return isLenMatch && isRight;
    }

    /**
     * 验证密码
     * 长度8-16位
     */
    public static boolean checkPassword(String pwd) {
//        boolean isLenMatch=false;
//        if (checkNotEmputy(pwd) && pwd.length() >= 8 && pwd.length() <= 16) {
//            isLenMatch=true;
//        }
//        return isLenMatch && isLetterDigit(pwd);

        //udpate:修改后不考虑是否为纯数字和纯字母的情况

        boolean isLenMatch=false;
        if (checkNotEmputy(pwd) && pwd.length() >= 8 && pwd.length() <= 16) {
            isLenMatch=true;
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight =  pwd.matches(regex);
        return isLenMatch && isRight;
    }

    /**
     * 是否包含数字和字母
     * @param str
     * @return
     */
    public static boolean isLetterDigit(String str) {
        boolean isDigit = false;//定义一个boolean值，用来表示是否包含数字
        boolean isLetter = false;//定义一个boolean值，用来表示是否包含字母
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符
                isDigit = true;
            }
            if (Character.isLetter(str.charAt(i))) {  //用char包装类中的判断字母的方法判断每一个字符
                isLetter = true;
            }
        }

        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isDigit && isLetter && str.matches(regex);
        return isRight;

    }

    /**
     * 验证传真号码
     *
     * @param fax
     * @return
     */
    public static boolean checkFax(String fax) {
        String regex = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
        return check(fax, regex);
    }

    /**
     * 验证QQ号码
     *
     * @param QQ
     * @return
     */
    public static boolean checkQQ(String QQ) {
        String regex = "^[1-9][0-9]{4,} $";
        return check(QQ, regex);
    }
}